# Copyright (c) 2008-2009 Mikhail Chernov 
#
# This file is part of dcExplorer.
#
# dcExplorer is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# dcExplorer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with dcExplorer.  If not, see <http://www.gnu.org/licenses/>.

package dcExplorer::server::Response;
use strict;
use utf8;
use Error;
use XML::Parser::PerlSAX;
use Storable qw(dclone);

sub new
{
	my $proto = shift;
	my $class = ref($proto) || $proto;
	my $self = {};
	$self->{Results} = {};
	bless ($self, $class);
	my $xml = shift;
	my $parser = XML::Parser::PerlSAX->new(Handler => $self);
	$parser->parse($xml);
	return $self;
}

my $act;
my $description = 0;

sub start_element
{
	my ($self, $element) = @_;
	if ($element->{Name} eq 'act')
	{
		$self->{Results}->{$element->{Attributes}->{'id'}} = {
			'is-successful' => $element->{Attributes}->{'result'} eq 'ok', 
			'return-id' => $element->{Attributes}->{'return-id'}
		};
		$act = $self->{Results}->{$element->{Attributes}->{'id'}};
	}
	if ($element->{Name} eq 'error')
	{
		$act->{'error-id'} = $element->{Attributes}->{'id'};
	}
	if ($element->{Name} eq 'txt')
	{
		$description = 1;
	}
}


sub end_element
{
	my ($self, $element) = @_;
	if ($element->{Name} eq 'txt')
	{
		$description = 0;
	}
	if ($element->{Name} eq 'act')
	{
		$act = undef;
	}
}

sub characters
{
	my ($self, $element) = @_;
	if ($description)
	{
		$act->{'error-text'} = $element->{Data};
	}
}

1;
